﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using BusinessLayer;
using BoletoElectronicoDesktop.AbmClientes;

namespace BoletoElectronicoDesktop.AbmTarjetas
{

    public partial class FormTarjetasList : FormListDelete
    {
        public dsBoleto.ClienteRow Cliente;

        public FormTarjetasList(Util.Funcionalidad Use):base(Use)
        {
            InitializeComponent();
        }


        private int selectedId;
        public int SelectedId
        {
            get
            {
                try
                {

                    if ((dsBoleto.TarjetaRow)((DataRowView)(dgwTarjetas.CurrentRow.DataBoundItem)).Row != null)
                    {
                        selectedId = ((dsBoleto.TarjetaRow)((DataRowView)(dgwTarjetas.CurrentRow.DataBoundItem)).Row).IdTarjeta;
                    }
                }
                catch (Exception)
                {
                    selectedId = 0;
                }
                return selectedId;
            }
            set
            { selectedId = value; }
        }


        private void FormTarjetasList_Load(object sender, EventArgs e)
        {

        }
        public void setCliente()
        {
            txtCliente.Text = Cliente.Apellido + ", " + Cliente.Nombre;
        }

        private void btSeleccionar_Click(object sender, EventArgs e)
        {
            try
            {
                BuscadorCliente frm;
                if (this.MdiParent.MdiChildren.Any(f => f is BuscadorCliente))
                {
                    frm = (BuscadorCliente)this.MdiParent.MdiChildren.First(f => f is BuscadorCliente);
                }
                else
                {
                    frm = new BuscadorCliente(Util.Funcionalidad.ClienteConsulta, 1);
                    frm.MdiParent = this.MdiParent;
                }

                frm.Show();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void btBuscar_Click(object sender, EventArgs e)
        {
            buscar();
        }

        public void buscar()
        {
            OnDGWDeselectedItem();
            int numeroTarjeta = 0;
            if (!String.IsNullOrEmpty(txtNumTarjeta.Text))
                int.TryParse(txtNumTarjeta.Text, out numeroTarjeta);

            int idCliente = 0;
            if (Cliente != null)
                idCliente = Cliente.IdCliente;

            dsBoleto ds = BizTarjeta.GetTarjetas(numeroTarjeta, (dtFechaAlta.Checked? (DateTime?)dtFechaAlta.Value:null), idCliente);
            if (ds.Tarjeta.Count != 0)
            {
                dgwTarjetas.DataSource = ds.Tarjeta;
            }
            else
            {
                MessageBox.Show("No existen registros para el criterio de busqueda ingresado.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            try
            {
                FormAbmTarjetas frm;
                if (this.MdiParent.MdiChildren.Any(f => f is FormAbmTarjetas))
                {
                    frm = (FormAbmTarjetas)this.MdiParent.MdiChildren.First(f => f is FormAbmTarjetas);
                }
                else
                {
                    frm = new FormAbmTarjetas(0, Util.Funcionalidad.TarjetaAlta);
                    frm.MdiParent = this.MdiParent;
                }

                frm.Show();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            modificar();
        }

        private void modificar()
        {
            try
            {
                FormAbmTarjetas frm;
                if (this.MdiParent.MdiChildren.Any(f => f is FormAbmTarjetas))
                {
                    frm = (FormAbmTarjetas)this.MdiChildren.First(f => f is FormAbmTarjetas);
                }
                else
                {
                    frm = new FormAbmTarjetas(SelectedId, Util.Funcionalidad.TarjetaModificacion);
                    frm.MdiParent = this.MdiParent;
                }

                frm.Show();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        private void btnEliminar_Click(object sender, EventArgs e)
        {
            eliminar();
        }
        private void eliminar()
        {
            BizTarjeta.DeleteTarjeta(SelectedId);
            MessageBox.Show("La Tarjeta se ha eliminado");
            buscar();
        }

        private void OnDGWSelectedItem()
        {
            dsBoleto.TarjetaRow cr = (dsBoleto.TarjetaRow)((DataRowView)(dgwTarjetas.CurrentRow.DataBoundItem)).Row;
            switch (dgwTarjetas.CurrentCell.ColumnIndex)
            {
                case 1://Edit
                  //  modificar();
                    break;
                case 2://Delete
                    //eliminar();
                    break;

                default:
                    break;
            }
            btnEdit.Enabled = true;
            btnEliminar.Enabled = true;
        }
        private void OnDGWDeselectedItem()
        {
            btnEdit.Enabled = false;
            btnEliminar.Enabled = false;
            SelectedId = 0;
        }

        private void dgwTarjetas_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
                OnDGWSelectedItem();
        }

 
    }
}
